<?php

namespace App\Admin\Controllers;

use App\Models\Cashier;
use App\Models\Member;
use App\Models\MemberLevel;
use App\Models\Store;
use App\Models\StoreDetail;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class MemberController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '会员列表';
    protected $sex = [
        1 => '男',
        2 => '女'
    ];
    protected $type = [1 => '开通会员', 2 => '会员充值', 3 => '充值赠送'];
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Member());

        $grid->column('store.name', __('Store id'));
        $grid->column('name', __('Name'));
        $grid->column('sex', __('Sex'))->using($this->sex);
        $grid->column('birthday', __('Birthday'));
        $grid->column('number', __('Number'));
        $grid->column('mobile', __('Mobile'));
        $grid->column('memberLevel.name', __('Member level id'));
        $grid->column('balance', __('Balance'))->sortable();
        $grid->column('total_grand', __('Total grand'))->sortable();
        $grid->column('integral', __('Integral'));
        //$grid->column('cashier.name', __('Cashier id'));
        $grid->column('created_at', __('Created at'));

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();

            $filter->column(1/2, function ($filter) {
                $filter->like('name', '姓名');
                $filter->equal('store_id', '门店')
                    ->select(StoreDetail::setSelect());
            });
            $filter->column(1/2, function ($filter) {
                $filter->equal('member_level_id', '会员等级')
                ->select(MemberLevel::all()->pluck('name', 'id'));
                $filter->like('mobile', '移动电话');
            });


        });


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Member::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('store_id', __('Store id'));
        $show->field('name', __('Name'));
        $show->field('sex', __('Sex'));
        $show->field('birthday', __('Birthday'));
        $show->field('number', __('Number'));
        $show->field('mobile', __('Mobile'));
        $show->field('member_level_id', __('Member level id'));
        $show->field('balance', __('Balance'));
        $show->field('total_grand', __('Total grand'));
        $show->field('integral', __('Integral'));
        $show->field('cashier_id', __('Cashier id'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Member());

        $form->select('store_id', __('Store id'))->options(Store::all()->pluck('name', 'id'));
        $form->text('name', __('Name'));
        $form->radioButton('sex', __('Sex'))->options($this->sex)->required();
        $form->date('birthday', __('Birthday'));
        $form->text('number', __('Number'));
        $form->mobile('mobile', __('Mobile'));
        $form->select('member_level_id', __('Member level id'))
            ->options(MemberLevel::all()->pluck('name', 'id'));
        $form->currency('balance', __('Balance'))->symbol('￥');
        $form->currency('total_grand', __('Total grand'))->symbol('￥');
        $form->number('integral', __('Integral'));
        $form->select('cashier_id', __('Cashier id'))
            ->options(Cashier::all()->pluck('name', 'id'));

        return $form;
    }
}
